<template>
  <z-publicFrame>
    <template #header>
      <el-page-header @back="goBack" content="添加">
      </el-page-header>
    </template>
    <template #container>
      <div class="formStyle">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="left">
          <ZForm v-for="(item, index) in formView" :key="index" :form="item" v-model="form[item.model]"></ZForm>
          <el-form-item>
            <el-button type="primary" @click="handleAddWithFile">立即增加</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </z-publicFrame>
</template>

<script>
import PublicFrame from '../../components/CrudTempalte/PublicFrame'
import { formView, api, initSelect } from './config'
function init() {
  const form = {}
  const rules = {}
  for (let i = 0; i < formView.length; i++) {
    let formViewElement = formView[i]
    let initValue = formViewElement.initValue
    let rule = formViewElement.rule
    let model = formViewElement.model
    if (initValue) {
      initValue = formViewElement.initValue
    } else {
      initValue = ''
    }
    form[model] = initValue
    //  rule
    if (rule) {
      rules[model] = [
        {
          required: true,
          message: rule.message,
          trigger: 'blur'
        }
      ]
    }
  }
  return { form, rules }
}
const { form, rules } = init()
export default {
  name: 'add',
  components: {
    'z-publicFrame': PublicFrame
  },
  data() {
    return {
      api: {
        add: this.$api[api].add
      },
      rules,
      formView,
      form
    }
  },
  methods: {
    init() {
      for (let i = 0; i < this.formView.length; i++) {
        let formViewElement = this.formView[i]
        let initValue = formViewElement.initValue
        let rule = formViewElement.rule
        let model = formViewElement.model
        if (initValue) {
          initValue = formViewElement.initValue
        } else {
          initValue = ''
        }
        this.form[model] = initValue
        //  rule
        if (rule) {
          this.rules[model] = [
            {
              required: true,
              message: rule.message,
              trigger: 'blur'
            }
          ]
        }
      }
    },
    goBack() {
      this.$router.go(-1)
    },
    handleAdd() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        this.$httpTemplate({ url: this.api.add, paras: this.form }, data => {
          this.$router.go(-1)
        })
      })
    },
    handleAddWithFile() {
      console.log(this.form)
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        const formData = new FormData()
        for (let key in this.form) {
          formData.append(key, this.form[key])
        }
        const config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
        this.$http.post(this.api.add, formData, config).then(data => {
          if (data.data.status === 200) {
            this.$success(data.data.message)
          }
          this.goBack()
        })
      })
    },
    initSelect
  },
  created() {
    this.initSelect()
  }
}
</script>

<style scoped>
  .el-page-header {
    margin: 15px 0;
    overflow: hidden;
  }
  div.formStyle {
    margin-top: 30px;
    width: 600px;

  }

</style>
